﻿using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
public class MapEditorBase : MonoBehaviour
{
    public Shape shape;
    public SceneType sceneType;
    public MonoAttribute attr;
    public List<int> Params;

    private static MapRoom _mapRoom;

    [HideInInspector]
    public bool NotStartOnScene = false;

    public virtual void OnEnable()
    {
        _mapRoom = new MapRoom();
        _mapRoom.mapID = 0;
        _mapRoom.roomID = 0;
        _mapRoom.mapDoors = new MapDoor();
        _mapRoom.mapGrounds = new Dictionary<int, List<int>>();
        _mapRoom.mapGroundPos = new Dictionary<int, MapGround>();
        _mapRoom.roomSize = new RoomSize();
        transform.SetParent(GameObject.Find("SceneObjs").transform);
        StartCoroutine(Init());
    }
    IEnumerator Init()
    {
        yield return 1;
        if (!NotStartOnScene)
        {
            Init(gameObject.transform.position, attr);
        }
    }
    public virtual void Awake()
    {
    }
    public virtual void Update()
    {

    }
    public virtual void LateUpdate()
    {

    }

    public static MapRoom GetRoomData()
    {
        return _mapRoom;
    }

    public void Init(Vector3 StartPos,MonoAttribute MonoAttr)
    {
        int y = (int)(StartPos.y * 2);
        int x = (int)(StartPos.x * 2);
        int key = GetHashPosKey(y, x);

        if (sceneType == SceneType.HorWall || sceneType == SceneType.VerWall)
        {
            MapWall wall = new MapWall(StartPos, sceneType, Params);

            _mapRoom.mapWalls.Add(wall);
            return;
        }
        MapGround ground = new MapGround(StartPos, sceneType, MonoAttr, Params);
        _mapRoom.roomSize.minY = Mathf.Min(_mapRoom.roomSize.minY, y);
        _mapRoom.roomSize.maxY = Mathf.Max(_mapRoom.roomSize.maxY, y);
        _mapRoom.roomSize.minX = Mathf.Min(_mapRoom.roomSize.minX, x);
        _mapRoom.roomSize.maxX = Mathf.Max(_mapRoom.roomSize.maxX, x);
        if (_mapRoom.mapGroundPos.ContainsKey(key))
            return;
        _mapRoom.mapGroundPos.Add(key, ground);

    }
    private int GetHashPosKey(int y,int x)
    {
        int pre = x << 16;
        return (pre | y);
    }

    public virtual void OnDrawGizmos()
    {
        //if (shape == Shape.Circle)
        //{
        //    Gizmos.DrawSphere(transform.position, GetComponent<CircleCollider2D>().radius - 0.05f);
        //}
        //if (shape == Shape.Square)
        //{
            Gizmos.DrawCube(transform.position, new Vector3(GetComponent<BoxCollider2D>().size.x - 0.05f, GetComponent<BoxCollider2D>().size.y - 0.05f, 1));
        //}
    }

    [System.Serializable]
    //房间地板类
    public class MapGround
    {
        public bool IsPlayerIn;
        public Vector3 position;
        public SceneType sceneType;
        public MonoAttribute attr;
        public List<int> param = new List<int>();
        public MapGround(){ }
        public MapGround(Vector3 vec,SceneType sType,MonoAttribute mAttr = MonoAttribute.None,List<int> param = null)
        {
            position = vec;
            sceneType = sType;
            attr = mAttr;
            this.param = param;
        }
    }
    [System.Serializable]
    public class MapWall
    {
        public Vector3 position;
        public SceneType sceneType;
        public List<int> param = new List<int>();
        public MapWall() { }
        public MapWall(Vector3 vec, SceneType sType, List<int> param = null)
        {
            position = vec;
            sceneType = sType;
            this.param = param;
        }
    }
    public class RoomSize
    {
        public int minX;
        public int maxX;
        public int minY;
        public int maxY;
        public RoomSize() { }
        public RoomSize(RoomSize size)
        {
            minX = size.minX;
            minY = size.minY;
            maxX = size.maxX;
            maxY = size.maxY;
        }
    }

    public class MapDoor
    {
        public List<int> linkRoomID = new List<int>();
        public List<Vector3> doorPos = new List<Vector3>();
        public Dictionary<int,DoorDirection> doorDic = new Dictionary<int,DoorDirection>();
        public MapDoor()
        {
            linkRoomID = new List<int>();
            doorPos = new List<Vector3>();
            doorDic = new Dictionary<int, DoorDirection>();
        }
        public MapDoor(MapDoor door)
        {
            foreach(var item in door.linkRoomID)
            {
                linkRoomID.Add(item);
            }
            foreach (var item in door.doorPos)
            {
                doorPos.Add(item);
            }
            foreach (var item in door.doorDic)
            {
                doorDic.Add(item.Key, item.Value);
            }
        }
    }

    [System.Serializable]
    public class MapRoom
    {
        public bool roomClear;
        public int roomID;
        public int mapID;
        public RoomStatue roomStatue;
        public RoomSize roomSize = new RoomSize();
        public List<MapWall> mapWalls = new List<MapWall>();
        public Vector2 offsetPos = new Vector2();
        public MapDoor mapDoors = new MapDoor();
        //<纵坐标*2,横坐标*2升序>
        public Dictionary<int, List<int>> mapGrounds = new Dictionary<int, List<int>>();
        //<坐标，地板>
        public Dictionary<int, MapGround> mapGroundPos = new Dictionary<int, MapGround>();
        public MapRoom()
        {
            roomID = 0;
            roomSize = new RoomSize();
            mapWalls = new List<MapWall>();
            offsetPos = new Vector2();
            mapDoors = new MapDoor();
            mapGrounds = new Dictionary<int, List<int>>();
            mapGroundPos = new Dictionary<int, MapGround>();

        }
    }
    public enum RoomStatue
    {
        Empty,
        Enemy,
        Chest,
        Challenge,
        Unknown,
    }

    public class MapCorridor
    {
        public CorridorDirection direction;
        public List<MapWall> mapWalls = new List<MapWall>();
        //<纵坐标*2,横坐标*2升序>
        public Dictionary<int, List<int>> mapGrounds = new Dictionary<int, List<int>>();
        //<坐标，地板>
        public Dictionary<int, MapGround> mapGroundPos = new Dictionary<int, MapGround>();
        public Vector2Int doorKey = new Vector2Int();
        public Vector2Int linkRoomID = new Vector2Int();
    }
    public class MapEditor
    {
        public int roomLen;
        public int roomWide;
        public Dictionary<int, MapRoom> mapRooms = new Dictionary<int, MapRoom>();
        public List<MapCorridor> mapCorridors = new List<MapCorridor>();
    }
    public enum DoorDirection
    { 
        Below,
        Above,
        Left,
        Right
    }
    public enum CorridorDirection
    {
        Horizontal,
        Vertical
    }

}
